import { reactive } from "vue";
import { api } from 'boot/axios'
import { CommonApiPrefix } from 'src/common/const'
import { HooksReturnDataType } from "src/pages/clueManager/const";
import utils from "src/utils";
import { JigouLineType } from "src/pages/clueManager/const";
/**
 * @description 表格多选数据操作
 * @param {String} rowKey 当表格可以多选时，所指定的 id
 * */
export interface JigouParamsProps {
  courseid3?: string | number;
  cityid?: string | number;
  online?: string | number;
}

export const useJigouList = (params: JigouParamsProps | null = {}, type: string = HooksReturnDataType.TABLE) => {

  const list = reactive<any[]>([])
  const fetchData = async () => {
    try {
      const { data, code, msg }: CommonType.ResDataType = await api.get(`${CommonApiPrefix}/jigou/list`, { params });
      list.push(...(Array.isArray(data) ? utils.uniqueObjArray(data, 'jigouid') : [])
        .map((item: any) => {
          if (type === HooksReturnDataType.SELECT) {
            const { jigouid, jigouName } = item
            return {
              value: jigouid,
              label: jigouName
            }
          }
          return item
        })
      )
      const msg_failed = params?.online ? (params.online === JigouLineType.OFF_LINE ? `该地区没有线下机构` : `该地区没有线上机构`) : msg
      if (`${code}` !== '0') {
        utils.commonNotification({
          condition: `${code}` === '0',
          msg_failed,
          type_failed: 'warning'
        })
      }
    } catch (error) {
      console.log(error);
    }
  }
  fetchData();
  return [list];
};
